package com.anbinh.doctor.fragment.tinnhan;

import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.anbinh.doctor.R;
import com.anbinh.doctor.model.NotificationDetailt;
import com.anbinh.doctor.model.UserLogin;
import com.anbinh.doctor.request.controller.ServerController;
import com.anbinh.doctor.service.request.OnRequest;
import com.anbinh.doctor.utils.IRcallBak;
import com.anbinh.doctor.utils.Utils;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.assist.SimpleImageLoadingListener;

public class FragmentMessagesDetailt extends Fragment {
	private static FragmentMessagesDetailt fragment = null;
	private View mView;
	private UserLogin userLogin;
	protected ListView mGridView;
	private int currentPage = 0;
	private int pageSize = 10;
	private TextView lblName;
	private TextView lblTime;
	private TextView lblContent;
	private ImageView icon;
	private ImageLoader imageLoader;
	private View btnChitiet;
	protected String appointment_id;
	private IRcallBak iRcallBak;

	public static FragmentMessagesDetailt newInstance(Bundle bundle) {
		fragment = new FragmentMessagesDetailt();
		fragment.setArguments(bundle);
		return fragment;
	}

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		userLogin = Utils.getAccountModel(getActivity(), Utils.KEY_USER);
		imageLoader = ImageLoader.getInstance();
		imageLoader.init(ImageLoaderConfiguration.createDefault(getActivity()
				.getApplicationContext()));
		if (activity instanceof IRcallBak) {
			iRcallBak = (IRcallBak) activity;
		}
	}

	@Override
	public void onResume() {
		super.onResume();
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		mView = inflater.inflate(R.layout.fragment_message_detailt, null);
		return mView;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		mGridView = (ListView) mView.findViewById(R.id.listView1);
		lblName = (TextView) mView.findViewById(R.id.lblName);
		lblTime = (TextView) mView.findViewById(R.id.lblTime);
		lblContent = (TextView) mView.findViewById(R.id.lblContent);
		icon = (ImageView) mView.findViewById(R.id.icon);
		btnChitiet = (View) mView.findViewById(R.id.btnChitiet);
		btnChitiet.setVisibility(View.GONE);
		btnChitiet.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Bundle bundle = new Bundle();
				bundle.putInt(Utils.KEY, Utils.APPOINTMENT_DETAILT);
				bundle.putString(Utils.TITLE,
						getResources().getString(R.string.chi_tiet_lich_hen));
				bundle.putInt(Utils.KEY_ID, Integer.parseInt(appointment_id));
				bundle.putBoolean(Utils.CUSTOMBAR, false);
				Utils.getNewIntent(getActivity(), bundle);
			}
		});
		onLoadData();
	}

	public void onLoadData() {
		Utils.onLoadding(mView, true);
		int notification_id = getArguments().getInt(Utils.KEY_ID);
		ServerController.getInstance(getActivity()).requestNotificationDetailt(
				userLogin.getData().getToken(), notification_id, -1,
				new OnRequest<NotificationDetailt>() {

					@Override
					public void onSuccess(NotificationDetailt data) {
						Utils.onLoadding(mView, false);
						if (getActivity() == null)
							return;
						if (data.getCode() < 0)
							return;
						if (data.getNotificationContent()
								.getReply_for_notification_id() > 0) {
							Bundle bundle=new Bundle();
							bundle.putInt(Utils.KEY, Utils.REPLY_PATIENT);
							iRcallBak.onDisplay(data.getNotificationContent()
									.getReply_for_notification_id(), null);
						}
						appointment_id = data.getNotificationContent()
								.getAppointment_id();
						if (Integer.parseInt(appointment_id) > 0)
							btnChitiet.setVisibility(View.VISIBLE);

						lblName.setText(data.getNotificationContent()
								.getSent_by_name());
						lblTime.setText(data.getNotificationContent().getTime());
						lblContent.setText(Html.fromHtml(data
								.getNotificationContent().getContent()));

						Bitmap bmp = imageLoader.loadImageSync(data
								.getNotificationContent()
								.getSent_by_avatar_thumbs());
						if (bmp == null) {
							imageLoader.displayImage(data
									.getNotificationContent()
									.getSent_by_avatar_thumbs(), icon, Utils
									.getOptions(getActivity()),
									new SimpleImageLoadingListener() {
										@Override
										public void onLoadingStarted(
												String imageUri, View view) {
										}

										@Override
										public void onLoadingFailed(
												String imageUri, View view,
												FailReason failReason) {
										}

										@Override
										public void onLoadingComplete(
												String imageUri, View view,
												Bitmap loadedImage) {
										}
									});
						} else {
							icon.setImageBitmap(bmp);
						}
					}

					@Override
					public void onError(int errorCode, String message) {
						Utils.onLoadding(mView, false);

					}
				});
	}

}
